{extend name="base"/}
{block name="resources"}
<link rel="stylesheet" type="text/css" href="SHOP_CSS/goods_edit.css" />
<style>
    .ns-form {margin-top: 0;}
</style>
{/block}
{block name="main"}
<div class="layui-form ns-form">

    <div class="layui-form-item">
        <label class="layui-form-label "><span class="required">*</span>名称：</label>
        <div class="layui-input-block">
            <input type="text" name="goods_name" lay-verify="required" class="layui-input ns-len-long" autocomplete="off">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>销售价：</label>
        <div class="layui-input-block">
            <input type="text" name="price" placeholder="0.00" lay-verify="price" class="layui-input ns-len-short" autocomplete="off">
            <div class="layui-form-mid">元</div>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">排序：</label>
        <div class="layui-input-block">
            <input type="number" name="sort" class="layui-input ns-len-short" placeholder="0" autocomplete="off">
        </div>
        <div class="ns-word-aux">商品默认排序号为0，数字越大，排序越靠前，数字重复，则最新添加的靠前。</div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>状态：</label>
        <div class="layui-input-block">
            <input type="radio" name="goods_state" value="1" title="上架" checked lay-filter="goods_state">
            <input type="radio" name="goods_state" value="0" title="下架" lay-filter="goods_state">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">服务说明：</label>
        <div class="layui-input-inline ns-special-length">
            <script id="editor" type="text/plain" style="width:100%;height:500px;"></script>
        </div>
    </div>
    <script type="text/javascript" charset="utf-8" src="__STATIC__/ext/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="__STATIC__/ext/ueditor/ueditor.all.js"> </script>
    <script type="text/javascript" charset="utf-8" src="__STATIC__/ext/ueditor/lang/zh-cn/zh-cn.js"></script>

    <div class="layui-form-item">
        <label class="layui-form-label">限购：</label>
        <div class="layui-input-block">
            <input type="number" name="max_buy" placeholder="" lay-verify="max_buy" class="layui-input ns-len-short" autocomplete="off">
            <div class="layui-form-mid">件</div>
        </div>
        <div class="ns-word-aux">该限购为终身限购，0为不限购</div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>分销分佣比例：</label>
        <div class="layui-input-block">
            <input type="number" name="fenxiao_commission_rate" placeholder="0" value="0" lay-verify="fenxiao_commission_rate" class="layui-input ns-len-short" autocomplete="off">
            <div class="layui-form-mid">%</div>
        </div>
    </div>

    <div class="ns-form-row">
        <button type="button" class="layui-btn ns-bg-color" lay-submit lay-filter="save">保存</button>
        <button class="layui-btn layui-btn-primary" onclick="back()">返回</button>
    </div>
</div>
{/block}
{block name="script"}
<script>
    var goodsContent;//商品详情

    goodsContent = UE.getEditor('editor');

    layui.use('form', function() {
        var form = layui.form,
            repeat_flag = false; //防重复
        form.render();

        form.on('submit(save)', function(data) {

            var goods_content = goodsContent.getContent();

            if (goods_content == "") {
                layer.msg("请填写商品详情");
                return false;
            } else if (goods_content.length < 5 || goods_content.length > 25000) {
                $(".goods-nav ul li:eq(3)").click();
                layer.msg("商品描述字符数应在5～25000之间");
                return false;
            }

            data.field.goods_content = goods_content;//商品详情

            if(repeat_flag) return;
            repeat_flag = true;

            $.ajax({
                url: ns.url("admin/localgoods/addGoods"),
                data: data.field,
                dataType: 'JSON',
                type: 'POST',
                success: function(res) {
                    repeat_flag = false;

                    if (res.code == 0) {
                        layer.confirm('添加成功', {
                            title:'操作提示',
                            btn: ['返回列表', '继续添加'],
                            yes: function(){
                                location.href = ns.url("admin/shopcategory/lists")
                            },
                            btn2: function() {
                                location.href = ns.url("admin/shopcategory/addCategory")
                            }
                        });
                    }else{
                        layer.msg(res.message);
                    }
                }
            });
        });

        /**
         * 表单验证
         */
        form.verify({
            sorts: function(value){
                if (value == '') {
                    return;
                }
                if (value%1 != 0) {
                    return '排序数值必须为整数';
                }
                if (value < 0) {
                    return '排序数值必须为大于0';
                }
            },
            flo: function (value) {
                if (value == '') {
                    return;
                }
                var reg = /^(0|[1-9]\d*)(\s|$|\.\d{1,2}\b)/;
                if (!reg.test(value)) {
                    return '保证金不能小于0，可保留两位小数！'
                }
            }
        });

    });

    //跳转到店铺列表
    function back() {
        location.href = ns.url("admin/localgoods/lists")
    }
</script>
{/block}